<template>
  <!-- 员工查看页面 -->
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <el-tab-pane label="登录账户设置">
            <!-- 放置表单 -->
            <el-form
              ref="userInfo"
              :model="userInfo"
              :rules="rules"
              label-width="120px"
              style="margin-left: 120px; margin-top: 30px"
            >
              <el-form-item label="姓名:" prop="username">
                <el-input v-model="userInfo.username" style="width: 300px" />
              </el-form-item>
              <el-form-item label="密码:" prop="password2">
                <el-input
                  v-model="userInfo.password2"
                  style="width: 300px"
                  type="password"
                />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="toSaveUserDetailById">更新</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <el-tab-pane label="个人详情">
            <UserInfo />
          </el-tab-pane>
          <el-tab-pane label="岗位信息">
            <!-- 放置个人详情 -->
            <component :is="userComponent" />
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import UserInfo from '@/views/employees/components/user-info'
import { getUserDetailById, saveUserDetailById } from '@/api/employees'
import jobInfoVue from './components/job-info.vue'
export default {
  components: {
    UserInfo
  },
  data() {
    return {
      userComponent: jobInfoVue,
      userId: this.$route.params.id,
      userInfo: {
        username: '',
        password2: ''
      },
      rules: {
        username: [
          { required: true, message: '名称不能为空', trigger: 'blur' },
          { min: 2, max: 6, message: '请输入2-6位字符', trigger: 'blur' }
        ],
        password2: [
          { required: true, message: '名称不能为空', trigger: 'blur' },
          { min: 2, max: 6, message: '请输入2-6位字符', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.toGetUserDetailById()
  },
  methods: {
    // 获取数据
    async toGetUserDetailById() {
      // 进行校验后这行
      this.userInfo = await getUserDetailById(this.userId)
      // console.log(this.userInfo)
    },
    async toSaveUserDetailById() {
      try {
        await this.$refs.userInfo.validate()
        const data = { ...this.userInfo, password: this.userInfo.password2 }
        await saveUserDetailById(data)
        // 刷新页面
        this.toGetUserDetailById()
        this.$message.success('修改成功')
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style>
</style>
